package tgc.edu.tms.web.controller;

import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.tms.custom.AjaxResult;
import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.entity.Profession;
import tgc.edu.tms.entity.Student;
import tgc.edu.tms.entity.TbClass;
import tgc.edu.tms.entity.Teacher;
import tgc.edu.tms.service.ProfessionService;
import tgc.edu.tms.service.TbClassService;
import tgc.edu.tms.web.form.TbClassForm;
import tgc.edu.tms.web.form.TeacherForm;

@Controller
@RequestMapping(value = "/tbclass")
public class TbClassController extends CommonController<TbClass, Integer, TbClassForm> {

	@Autowired
	private TbClassService service;
	@Autowired
	private ProfessionService professionService;
	@Autowired
	private TbClassService tbClassService;

	@Override
	public void edit(TbClassForm form, ModelMap map) throws InstantiationException, IllegalAccessException {
		TbClass model = new TbClass();
		model = null;
		Integer id = form.getId();
		if (id != null) {
			System.out.println(id);
			model = service.findById(id);
			System.out.println(model.getNumber());
		}
		List<String> years = professionService.findAllYear();
		map.put("years", years);
		map.put("model", model);
	}

	@Override
	public Object save(TbClassForm form) {
		try {
			TbClass model = new TbClass();
			Integer id = form.getId();
			if (id != null) {
				model = service.findById(id);
			}
			BeanUtils.copyProperties(form, model, "id");
			Profession profession = professionService.findByYearAndCollegeAndProfesion(form.getYear(),
					form.getCollege(), form.getProfesion()); // 根据form对应的专业中字段查找出专业信息
				TbClass tblass=tbClassService.findByNameAndProfession(profession.getId(),form.getName());
			if(tblass!=null) {
				return new AjaxResult(false,"该班已存在！");
			}
			model.setProfession(profession);
			service.save(model);
			return new AjaxResult("OK");
		} catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
	}

	@Override
	public Specification<TbClass> buildSpec(TbClassForm form) {
		Specification<TbClass> spec = new Specification<TbClass>() {

			@Override
			public Predicate toPredicate(Root<TbClass> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(form.getYear())) {
					rules.add(cb.like(root.get("profession").get("year"), "%"+form.getYear()+"%"));
				}
				if(StringUtils.hasText(form.getProfesion())) {
					rules.add(cb.like(root.get("profession").get("profesion"), "%"+form.getProfesion()+"%"));
				}
				if(StringUtils.hasText(form.getName())) {
					rules.add(cb.like(root.get("name"), "%"+form.getName()+"%"));
				}
				if(StringUtils.hasText(form.getSearch())) {
					Predicate like = cb.like(root.get("name"),"%"+form.getName()+"%");
					Predicate or = cb.or(like);
					rules.add(or);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
			
		};
		return spec;
	}

	/**
	 * 根据年级获取学院
	 * 
	 * @param year
	 * @return
	 */
	@RequestMapping(value = "/getCollege", method = RequestMethod.POST)
	@ResponseBody
	public Object getCollege(String year) {
		return professionService.findCollegeByYear(year);
	}

	/**
	 * 根据年级和学院获取专业
	 * 
	 * @param year
	 * @param college
	 * @return
	 */
	@RequestMapping(value = "/getProfesion", method = RequestMethod.POST)
	@ResponseBody
	public Object getProfesion(String year, String college) {
		return professionService.findProfessionByYearAndCollege(college, year);
	}

	//查询时加载所有年级
	@RequestMapping(value="/year")
	@ResponseBody
	public Object year() {
		List<String> findAll = professionService.findAllYear();
		return findAll;
	}
	
	//根据年级查询所有专业
	@RequestMapping(value="/profesion")
	@ResponseBody
	public Object profesions(String year) {//查询选择的参数
		return professionService.findProfesionByYear(year);
	}
	
	//根据年级和专业查找所有班级
		@RequestMapping(value="/banji")
		@ResponseBody
		public Object banji(String year,String profesion) {
			Integer professionid = professionService.findIdByProfesionAndYear(profesion, year);
			return tbClassService.findByprofessionID(professionid);
		}
}
